<template>
  <a-card :bordered="false">

    <div class="table-operator" :md="24" :sm="24" style="margin: 0px 0px 20px 0px">
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        :data="{'id': this.queryParam.mainId,'type': 2}"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button type="primary" icon="download" @click="handleExportXls('出库明细模板.xlsx')">下载模板</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
        selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">入库</a>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <StmStockInoutItem-modal ref="modalForm" @ok="modalFormOk"/>

    <stm-stock-inout-item-add-modalJX ref="addModalForm" @ok="modalFormOk"/>
  </a-card>
</template>

<script>
  import StmStockInoutItemModal from './modules/StmStockInoutItemModal'
  import StmStockInoutItemAddModalJX from './modules/StmStockInoutItemAddModalJX'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { getAction ,downFile} from '@/api/manage'

  export default {
    name: 'StmStockInItemListJX',
    mixins: [JeecgListMixin],
    components: {
      StmStockInoutItemModal,
      StmStockInoutItemAddModalJX
    },
    data() {
      return {
        importExcelUrl:`${window._CONFIG['domianURL']}/stm/stmStockInout/importExcel`,
        description: '信息',
        // 表头
        columns: [
          {
            title: '商品名称',
            align: 'center',
            dataIndex: 'productName'
          },
          {
            title: '货号',
            align: 'center',
            dataIndex: 'productSn'
          },
          {
            title: '物料编码',
            align: 'center',
            dataIndex: 'productSkuCode'

          },
          {
            title: '申请数量',
            align: 'center',
            dataIndex: 'productQuantity'
          },
          {
            title: '出库数量',
            align: 'center',
            dataIndex: 'sendQty'
          }],
        url: {
          list: '/stm/stmStockInout/queryOrderItemlistByRelationId',
          exportXls: '/stm/stmStockInout/download'
        }
      }
    },
    methods: {
      loadData(arg) {
        if (arg === 1) {
          this.ipagination.current = 1
        }
        var params = this.getQueryParams()
        getAction(this.url.list, {
           id: params.mainId
          , pageNo: this.ipagination.current, pageSize: this.ipagination.pageSize
        }).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          } else {
            this.dataSource = null
          }
        })
      },
      handleExportXls(fileName) {
        let param = { ...this.getQueryParams() }
        if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
          param['selections'] = this.selectedRowKeys.join(',')
        }
        console.log('导出参数', param)
        downFile(this.url.exportXls, param).then((data) => {
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName)
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName)
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      },
      getMain(mainId) {
        this.queryParam.mainId = mainId
        this.loadData(1)
      },
      handleAdd: function () {
        this.$refs.addModalForm.add(this.queryParam.mainId);
        this.$refs.addModalForm.title = "添加出入库明细";
      },
      handleEdit: function (record) {
        this.$refs.addModalForm.edit(record);
        this.$refs.addModalForm.title = "入库";
        this.$refs.addModalForm.disableSubmit = false;
      },
      handleDetail:function(record){
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title="详情";
        this.$refs.modalForm.disableSubmit = true;
      },
    }
  }
</script>
<style scoped>
  .ant-card {
    margin-left: -30px;
    margin-right: -30px;
  }
</style>
